<?php
header('Content-Type: text/xml');
?> 
<?php 

include("../db/opendb.php");

include("functions.php");

$query1 = "select AVG(Price_Per_SQFT) as avg from Game_Data where City = 'San Francisco' and Property_Type = 'RESI' and Selling_Date >= DATE_SUB(now(), INTERVAL 3 month);";
$result1 = mysql_query($query1) or die ("SQL Error".mysql_error());
$row1 = mysql_fetch_assoc($result1);
//echo $row1['avg'];

$query = "select Listing_Number, Price_Per_SQFT, Longitude, Latitude, Address, Current_Price, Beds, Baths, SQFT, Status from Game_Data where Price_Per_SQFT < (.8*".$row1['avg'].") and Price_Per_SQFT > 0 and Status in ('A', 'ACTIVE', 'C') and Property_Type = 'RESI' and City = 'San Francisco'";

$result = mysql_query($query) or die ("SQL Error".mysql_error());

$xml = "<markers>";

while ($row = mysql_fetch_assoc($result))
{
	$xml .= "<marker lng='".$row[Longitude]."' lat='".$row[Latitude]."'>";
	$xml .= "<id><![CDATA[".$row['Listing_Number']."]]></id>";
	$xml .= "<info><![CDATA[".$row['Address']."]]></info>";
	$xml .= "<beds><![CDATA[".$row['Beds']."]]></beds>";
	$xml .= "<baths><![CDATA[".$row['Baths']."]]></baths>";
	$xml .= "<price><![CDATA[$".number_format($row['Current_Price'], 0)."]]></price>";
	$xml .= "<apps><![CDATA[".number_format(($row['Price_Per_SQFT']/$row1['avg'])*100, 2)."%]]></apps>";
	$xml .= "<pps><![CDATA[".$row['Price_Per_SQFT']."]]></pps>";
	$xml .= "</marker>";
}
$xml .= "</markers>";

echo $xml;

?>
